package com.ldg.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.ld.utils.PageUtils;
import com.ldg.manager.dao.ItemDao;
import com.ldg.manager.entity.ItemEntity;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.utils.Query;

import com.ldg.manager.dao.GoodsDao;
import com.ldg.manager.entity.GoodsEntity;
import com.ldg.manager.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {
    @Autowired
    private ItemDao itemDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void updateStatus(String id, String status) {
        //创建要修改的商品对象
        GoodsEntity goods = new GoodsEntity();
        //为商品对象赋值
        goods.setId(id);
        goods.setAuditStatus(status);
        //修改商品
        baseMapper.updateById(goods);
        //根据goodsid查询出sku商品列表
        List<ItemEntity> itemEntities = itemDao.selectList(new QueryWrapper<ItemEntity>().eq("goods_id", id));
        //发送消息
        rabbitTemplate.convertAndSend("updateStatus", JSON.toJSONString(itemEntities));
    }
}
